#include "common.h"
#include "bsp_flash.h"
#include "app_flash.h"
#include "define.h"
/****************************************************************
*Name:      WritePartNumToFlash
*Function:  把参数写入flash中
*Input:     无
*Output:    无
*Author:    gm_jeng
*Date:      2020/3/7
*E-Mail:    gm_jeng@163.com
*Other:
****************************************************************/
void WritePartNumToFlash(void)
{
    uint32_t MyInformation = 0x61008888;
    uint32_t TempData;
    do{
        Erase_Parameter_Sector(FLASH_INFORMATION_ADDR);
        EFRFLASH_Write(FLASH_INFORMATION_ADDR,(uint32_t*)&MyInformation,1);
        ReadFlashNBtye(FLASH_INFORMATION_ADDR,(uint8_t*)&TempData,0x04);
    }
    while(TempData != MyInformation);
}

/****************************************************************
*Name:      ReadPartNumFromPartNum
*Function:  从flash中读出参数
*Input:     无
*Output:    无
*Author:    gm_jeng
*Date:      2020/3/7
*E-Mail:    gm_jeng@163.com
*Other:
****************************************************************/
extern DataFrame Data;
void ReadPartNumFromPartNum(void)
{
	uint8_t addr[4] = {0};

    ReadFlashNBtye(FLASH_INFORMATION_ADDR,&addr[0],0x04);

    Data.LoPartNo[0] = addr[3];
    Data.LoPartNo[1] = addr[2];
    Data.LoPartNo[2] = addr[1];
    Data.LoPartNo[3] = addr[0];
}

/****************************************************************
*Name:      WriteReadPartNum
*Function:  读写partNo
*Input:     无
*Output:    无
*Author:    gm_jeng
*Date:      2020/3/7
*E-Mail:    gm_jeng@163.com
*Other:
****************************************************************/
void WriteReadPartNum(void)
{

#if 0
    WritePartNumToFlash();
#endif
    ReadPartNumFromPartNum();
}


/****************************************************************
*Name:      OTAFlashOperation
*Function:  升级参数设置函数
*Input:     NULL
*Output:    NULL
*Author:    gm_jeng
*Date:      2020/3/10
*E-Mail:    gm_jeng@163.com
*Other:
****************************************************************/
extern OTAParaFame ota;

void OTAFlashOperation(void)
{
    unsigned char BufPara[32]={0};
    memset(&BufPara[0], 0x0, sizeof(BufPara));
    BufPara[0]=ota.para.ota_state;            //升级状态
    memcpy(&BufPara[1], &ota.para.ota_version0[0], 3);   //version0及其块号
    memcpy(&BufPara[4], &ota.para.ota_version1[0], 3);   //version1及其块号
    memcpy(&BufPara[7], &ota.para.ota_data_length[0], 3); //待升级的firmware长度
    memcpy(&BufPara[10], &ota.para.ota_data_crc[0], 4);  //firmware校验码
    memcpy(&BufPara[14], &ota.para.ota_end_data[0], 16);  //firmware校验码
    MSC_ErasePage((uint32_t *)FLASH_OTA_ADDR);
    EFRFLASH_Write(FLASH_OTA_ADDR, (uint32_t*)&BufPara[0], 32 / 4);
}
